home *** CD-ROM | disk | FTP | other *** search
/ Business & Presentations / Business and Presentations - Volume 1 (1995)(Sideface)(NL).iso / hputils / djprnt / line.cpp < prev    next >
C/C++ Source or Header  |  1990-02-10  |  2KB  |  58 lines

  1. /*
  2.     LINE.CPP
  3.     Copyright (c) Les Hancock 1990
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stream.hpp>
  9. #include "list.hpp"
  10. #include "parms.hpp"
  11. #include "line.hpp"
  12. #include <stdlib.h>
  13.  
  14. /*
  15.     Friend function. Use input stream to fill a line exactly <size> bytes long.
  16. */
  17. istream& operator>>(istream& in, line& a_line)
  18. {
  19.     register unsigned int n = a_line.size;
  20.     register char *ptr = a_line.head;
  21.     while (n > 0)                    // while the line's not actually filled up
  22.     {
  23.         char ch;
  24.         in.get(ch);
  25.         if (in.bad() || in.eof() || in.fail())   // if can't read more from file
  26.         {
  27.             if (ptr == a_line.head)                // if we got nothing this time
  28.                a_line.eof = 1;                      // set the flag that shows eof
  29.            break;
  30.         }
  31.         switch(ch)                            // what was that character anyway?
  32.         {
  33.             case '\t':       // expand tab if enough room on line, else push back
  34.                 if (n > a_line.tab_size)
  35.                  {
  36.                     memset(ptr, ' ', a_line.tab_size);
  37.                     ptr += a_line.tab_size;
  38.                     n -= a_line.tab_size;
  39.                 }
  40.                 else
  41.                 {
  42.                     in.putback(ch);             // too big to handle, end line here
  43.                     goto end_of_the_line;
  44.                 }                                                 // falls through
  45.             case 0xd:                      // ignore CR, which comes in by itself
  46.                 break;
  47.             case 0xa:                                       // signals end of line
  48.                 goto end_of_the_line;
  49.             default:                        // any other character, append to buffer
  50.                 *ptr++ = ch;
  51.                 --n;
  52.         }
  53.     }
  54. end_of_the_line:
  55.     memset(ptr, ' ', n);                          // ... set blanks to end of line
  56.     return in;
  57. }
  58.